//
//  JCutHairHeadView.m
//  HeadIndustry
//
//  Created by jun on 2017/9/20.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JCutHairHeadView.h"

@interface JCutHairHeadView()<UITableViewDelegate , UITableViewDataSource, UIGestureRecognizerDelegate>

@end

@implementation JCutHairHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            if (_cancelBlock) {
                _cancelBlock();
            }
        }];
        tapGesture.delegate = self;
        tapGesture.cancelsTouchesInView = NO;
        [self addGestureRecognizer:tapGesture];
        
        [self setUpView];
    }
    return  self;
}

- (void)setUpView {
    
    UITableView *tableViewObj = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, 0, 0) style:UITableViewStylePlain];
    tableViewObj.dataSource = self;
    tableViewObj.delegate = self;
    [self addSubview:tableViewObj];
    tableViewObj.bounces = NO;
    _tableView = tableViewObj;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (_dataArr.count > indexPath.row) {
//        JProductTypeModel *typeModel = _dataArr[indexPath.row];
        cell.textLabel.text = _dataArr[indexPath.row];
    }
//    if (indexPath.row == 0) {
//        CALayer *line = [CALayer layer];
//        line.backgroundColor = kRGBColor(150, 150, 150).CGColor;
//        line.frame = CGRectMake(0, 0 , cell.contentView.width, 0.5);
//        [cell.contentView.layer addSublayer:line];
//    }
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_productTypeBlock) {
        _productTypeBlock(_dataArr[indexPath.row], indexPath.row);
    }
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    [self.tableView reloadData];
}

//- (void)setConfigTableViewFrame
//{
//    if (_dataArr.count > 0) {
//        self.tableView.frame = CGRectMake(0, 0, self.width, self.height);
//    }
//}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}

@end
